<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{
    protected $noNeedLogin=['*'];
    //构造函数
    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
    }


    public function index()
    {
        // 课程分级顺序
        $CategoryModel = model('Subject.Category');
        $cate = $CategoryModel->order("weight ASC")->select();
        $cate = collection($cate)->toArray();
        // 轮播图循环顺序
        $SubjectModel = model('Subject.Subject');
        $toplist = $SubjectModel->orderRaw("LPAD(LOWER(likes),10,0)DESC")->select();
        $toplist = collection($toplist)->toArray();
        
        $catelist = [];
        // 将分级课程循环一遍拿到id
        foreach($cate as $key=>$item){

            // 左联拿到课程数据
            $subject = $SubjectModel->where(['cateid'=>$item['id']])->order('createtime DESC')->select();
            $subject = collection($subject)->toArray();

            // 有课程才追加分类
            if($subject){
                $catelist[] = [
                    'id' => $item['id'],
                    'name' => $item['name'],
                    'subject' => $subject,
                ];
            }
        }
        $this->view->assign([
            'catelist' => $catelist,
            'toplist' => $toplist,
        ]);

        return $this->view->fetch();
    }
    // 登录页面
    public function login()
    {
        return $this->view->fetch();
    }
    // 注册页面
    public function register()
    {
        return $this->view->fetch();
    }
    // 登录提交
    public function login_from(){
        if($this->request->isPost()){
            $mobile = $this->request->param('username','','trim');
            $pass = $this->request->param('pass','','trim');
            if(empty($mobile)){
                $this->error('用户名不能为空');
                exit;
            }
            if(empty($pass)){
                $this->error('密码不能为空');
                exit;
            }
            $business = $this->BusinessModel->where(['mobile' => $mobile])->find();
            $business = $business->toArray();
            if(!$business){
                $this->error('用户不存在，请注册','/index/Index/login');
                exit;
            }
            $salt = $business['salt'];
            $password = md5($pass.$salt);
            if($password != $business['password']){
                $this->error('密码不正确，请重新输入');
                exit;
            }
            $data = [
                'id' => $business['id'],
                'mobile' => $business['mobile'],
                'nickname' => $business['nickname'],
                'avatar' => $business['avatar'],
                'deal' => $business['deal']
            ];
            $data['mobile_text'] = substr_replace($data['mobile'],'****',3,4);
            cookie('LoginUser',$data);

            if($this->request->isGet()){
                $subid = $this->request->param('subid');
                $this->success('登录成功','/index/subject/subject',['subid'=>$subid]);
                exit;
            }
            $this->success('登录成功','/index/business.business/index');
            exit;
        }
    }
    // 注册提交
    public function register_from(){
        
        if($this->request->isPost()){
            $salt = build_ranstr();
            $mobile = $this->request->param('username','','trim');
            $pass = $this->request->param('pass','','trim');
            $pass_two = $this->request->param('pass_two','','trim');
            if($pass != $pass_two){
                $this->error('确认密码输入不一致，请重新输入');
                exit;
            }
            $password = md5($pass.$salt);
            $data=[
                'mobile' => $mobile,
                'nickname' => $mobile,
                'password' => $password,
                'salt' => $salt,
                'gender' => '0',
                'deal' => '0',
                'invitecode' => $salt,
                'sourceid' => null
            ];
            $result = $this -> BusinessModel->validate('common/Business/Business')->save($data);
            if($result === false){
                $this->error($this->BusinessModel->getError());
                exit;
            }else{
                $this->success('注册成功，请登录',url('index/Index/index'));
                exit;
            }
        }
    }
    // 退出登录
    public function clearCookie(){
        cookie('LoginUser',null);
        $this->success('退出成功','/index/index/login');
        exit;
    }

}
